﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PowerView.Page
{
    public partial class QC_Form : Form
    {
        byte[] data_buf = null;
        const byte QC_Protocol = 0x30;
        const UInt16 CMD_ShakeHand = 0x0000;
        const UInt16 CMD_Volt_Set  = 0x0001;
        const UInt16 CMD_Continue  = 0x0002;

        public QC_Form()
        {
            InitializeComponent();
            data_buf = new byte[128];
        }

        public void CMD_Proc(byte[] cmd)
        {
            switch (cmd[3])
            {
                case 0x00: // 握手
                    break;
                case 0x01: // 固定档位调压
                    //Volt_Btn_Color(cmd[5]);
                    break;
                case 0x02: // 连续模式调压
                    break;

            }
        }

        private void Volt_Btn_Color(byte volt)
        {
            Volt_5V_Btn.BackColor = MiddleWare.default_color;
            Volt_9V_Btn.BackColor = MiddleWare.default_color;
            Volt_12V_Btn.BackColor = MiddleWare.default_color;
            Volt_20V_Btn.BackColor = MiddleWare.default_color;
            Volt_Continue_Btn.BackColor = MiddleWare.default_color;

            switch (volt)
            {
                case 5:
                    Volt_5V_Btn.BackColor = Color.LightGreen;
                    break;
                case 9:
                    Volt_9V_Btn.BackColor = Color.LightGreen;
                    break;
                case 12:
                    Volt_12V_Btn.BackColor = Color.LightGreen;
                    break;
                case 20:
                    Volt_20V_Btn.BackColor = Color.LightGreen;
                    break;
                case 0:
                    Volt_Continue_Btn.BackColor = Color.LightGreen;
                    break;
            }

        }

        private void ShakeHand_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, QC_Protocol, 0x00, data_buf, 0);
        }

        private void Volt_5V_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = 5;
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Volt_Set, data_buf, 1);
        }

        private void Volt_9V_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = 9;
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Volt_Set, data_buf, 1);
        }

        private void Volt_12V_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = 12;
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Volt_Set, data_buf, 1);
        }

        private void Volt_20V_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = 20;
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Volt_Set, data_buf, 1);
        }

        private void Volt_Continue_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = 0;
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Volt_Set, data_buf, 1);
        }

        private void Volt_Up_Click(object sender, EventArgs e)
        {
            data_buf[0] = (byte)(0x10 + Up_Numeric.Value);
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Continue, data_buf, 1);
        }

        private void Volt_Down_Btn_Click(object sender, EventArgs e)
        {
            data_buf[0] = (byte)(0x00 + Down_Numeric.Value);
            MiddleWare.Write_CMD(0xff, QC_Protocol, CMD_Continue, data_buf, 1);
        }
    }
}
